package com.cskaoyan.weixin.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.CouponUser;
import com.cskaoyan.mapper.CouponMapper;
import com.cskaoyan.weixin.bean.coupon.CodeBo;
import com.cskaoyan.weixin.bean.coupon.Coupon;
import com.cskaoyan.weixin.bean.coupon.CouponSelectListVO;
import com.cskaoyan.weixin.bean.coupon.CouponidBO;
import com.cskaoyan.weixin.service.CouponService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("wx/coupon")
public class WxcouponController {
    @Autowired
    CouponService couponService;
    @RequestMapping("list")
    public BaseRespVo list(Integer page,Integer size){
        Coupon coupon=couponService.couponlist(page,size);
        return BaseRespVo.ok(coupon);
    }
    @RequestMapping("mylist")
    public BaseRespVo mylist(Integer status,Integer page,Integer size){
        Coupon coupon=couponService.couponmylist(status,page,size);
        return BaseRespVo.ok(coupon);
    }
    @RequestMapping("selectlist")
    public BaseRespVo selectlist(Integer cartId,Integer grouponRulesId){
        List<CouponSelectListVO> couponselectlist = couponService.couponselectlist(cartId, grouponRulesId);
        return BaseRespVo.ok(couponselectlist);
    }
    @RequestMapping("receive")
    public BaseRespVo receive(@RequestBody CouponidBO couponid ){
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        if (principals == null) {
            return BaseRespVo.fail(501, "请登录");
        }
        Integer couponId=couponid.getCouponId();
        Integer receive=couponService.receive(couponId);
        if (receive==1){
        return BaseRespVo.ok();
        }else{
            return BaseRespVo.fail();
        }
    }
    @RequestMapping("exchange")
    public BaseRespVo exchange(@RequestBody CodeBo codeBo){
        String code = codeBo.getCode();
        if (code == null) {
            return BaseRespVo.fail(10000, "兑换码不能为空");
        }
        Integer integer=couponService.exchange(code);
        return integer == 1 ? BaseRespVo.ok() : BaseRespVo.fail("系统内部错误");

    }
}

